const svc = require('../util/service'),
log = require('../util/log'),
wordDao = require('../dao/wordDao'),
uwDao = require('../dao/urlWordDao'),
urlDao = require('../dao/urlDao'),
upt = require('../task/updatePTask');

module.exports = (req, res) => {
	svc.handlePost(req, res, (reqJSON, fn) => {
		if (!reqJSON.word) {
			fn({
				result : -1,
				msg : '缺少word'
			})
			return
		}
		var sWord = reqJSON.word.replace(
				/[^\u4e00-\u9fa5\w\d]/gi, '').toLowerCase(),
		page = reqJSON.page ? reqJSON.page : 1;
		if (sWord == '') {
			fn({
				urls : []
			})
			return
		}
		if (!req.session.searchWords)
			req.session.searchWords = [];
		new Promise(resolve => {
			wordDao.findByWord(sWord, word => {
				if (word != null) {
					req.session.searchWords.push(word)
					uwDao.searchUrl(word.wordId, page, uws => {
						resolve(uws)
					})
				} else {
					word = {
						word : sWord,
						p : 0
					}
					var p1 = new Promise(resolve => {
							wordDao.insert(word, () => {
								req.session.searchWords.push(word)
								resolve(word)
							})
						}),
					p2 = new Promise(resolve => {
							wordDao.matchContainWord(sWord, words => {
								var selectUrlwordPromises = [];
								words.forEach(word => {
									selectUrlwordPromises.push(new Promise(resolve => {
											uwDao.matchWordId(word.wordId, uws => {
												resolve(uws)
											})
										}))
								})
								Promise.all(selectUrlwordPromises).then(uwss => {
									resolve(uwss)
								}).catch (err => {
									log(err)
								})
							})
						});
					Promise.all([p1, p2]).then(result => {
						var word = result[0],
						uwss = result[1],
						insertUrlwordPromises = [];
						uwss.forEach(uws => {
							uws.forEach(uw => {
								uw.wordId = word.wordId;
								insertUrlwordPromises.push(new Promise(resolve => {
										uwDao.insert(uw, () => {
											upt.add(uw, () => {
												resolve(uw)
											})
										})
									}))
							})
						})
						Promise.all(insertUrlwordPromises).then(uws => {
							resolve(uws)
						}).catch (err => {
							log(err)
						})
					}).catch (err => {
						log(err)
					})
				}
			})
		}).then(uws => {
			var selectUrlPromises = []
			uws.forEach(uw => {
				selectUrlPromises.push(new Promise(resolve => {
						var p1 = new Promise(resolve => {
								urlDao.findByUrlId(uw.urlId, url => {
									resolve({
										url : url.url,
										title : url.title,
										p : url.p
									})
								})
							}),
						p2 = new Promise(resolve => {
								uwDao.searchWord(uw.urlId, uws => {
									var selectWordPromises = []
									uws.forEach(uw => {
										selectWordPromises.push(new Promise(resolve => {
												wordDao.findByWordId(uw.wordId, word => {
													resolve({
														word : word.word,
														p : uw.p
													})
												})
											}))
									})
									Promise.all(selectWordPromises).then(words => {
										resolve(words)
									})
								})
							});
						Promise.all([p1, p2]).then(result => {
							var url = result[0],
							words = result[1];
							url.words = words;
							resolve(url)
						})
					}))
			})
			Promise.all(selectUrlPromises).then(urls => {
				fn({
					urls
				})
			})
		})
	})
}
